package com.fbinggun.dt.task.conditions;

import com.fbinggun.dt.task.Task;
import com.fbinggun.fbinggunutil.ItemUtil;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Fish extends Conditions{

    List<String> list = new ArrayList<>();
    public Fish(String id, ConfigurationSection config, Task task) {
        super(id, config, task);
        list=config.getStringList("Type");
    }


    @EventHandler
    public void fish(PlayerFishEvent event) throws SQLException, ClassNotFoundException {
        Player player = event.getPlayer();
        if(isRun(player)){
            if(event.getState()!= PlayerFishEvent.State.CAUGHT_FISH){
                return;
            }
            Entity entity= event.getCaught();
            if(entity==null){
                return;
            }
            if(!(entity instanceof Item)){
                return;
            }
            Item itemEntity = (Item) entity;
            ItemStack itemStack= itemEntity.getItemStack();
            String itemType = ItemUtil.getType(itemStack);
            if (list == null || list.size() == 0) {
                addCount(player, 1);
            } else {
                if (list.contains(itemType)) {
                    addCount(player, 1);
                }
            }
        }


    }
}
